
var findStuInfo = toContextPath('/student/student_getInfo.action');
var findExam = toContextPath('/student/exam_getAll.action');
var findQuiz = toContextPath('/student/quiz_getAll.action');
var findHomework = toContextPath('/student/homework_getAll.action');
var loginHtml = toContextPath('/StuPages/login.html');


var examState;
var quizState;
var hwState;
$(function(){
	
	initPage();
});


function initPage() {
	
	location.hash = '';
	$.getJSON(findStuInfo, function(data) {
		if (data.result && data.result.stuName){
			$('#stuName').text(data.result.stuName);
		} else {
			window.location.href = loginHtml;
		}
	});
	$.getJSON(findExam, function(data) {
		if (data.result && data.result.length > 0){
			var examhead = '<tr><td><h6>Exams</h6></td></tr>' + '<tr>'
                			+ '<th scope="col">&nbsp;BeginTime</th>'
                			+ '<th scope="col">&nbsp;EndTime</th>'
                			+ '<th scope="col">&nbsp;ExamName</th>'
                			+ '<th scope="col">&nbsp;Publisher</th>'
                			+ '<th scope="col">&nbsp;State</th>' +'</tr>';
			$('#todoList').append(examhead);
			$.each(data.result, function(index, exam) {
				if (exam.state == 0) {
					examState = "Todo";
				} else if (exam.state == 1) {
					examState = "Doing";
				} else if (exam.state == 2) {
					examState = "Done";
				}
				var item = '<tr class="warning">'
					+ '<td>' + exam.examBeginDate + '</td>'
					+ '<td>' + exam.examEndDate + '</td>'
					+ '<td>' + exam.examName+ '</td>'
					+ '<td>' + exam.teaName + '</td>'
					+ '<td style="color:red">' + examState + '</td>'+ '</tr>';
				$('#todoList').append(item);
			});
		}
	});

	$.getJSON(findQuiz, function(data) {
		if (data.result && data.result.length > 0){
			var quizhead = '<tr><td><h6>Quizzes</h6></td></tr>' + '<tr>'
                			+ '<th scope="col">&nbsp;BeginTime</th>'
                			+ '<th scope="col">&nbsp;EndTime</th>'
                			+ '<th scope="col">&nbsp;QuizName</th>'
                			+ '<th scope="col">&nbsp;Publisher</th>'
                			+ '<th scope="col">&nbsp;State</th>' +'</tr>';
			$('#todoList').append(quizhead);
			$.each(data.result, function(index, quiz) {
				if (quiz.state == 0) {
					quizState = "Todo";
				} else if (quiz.state == 1) {
					quizState = "Doing";
				} else if (quiz.state == 2) {
					quizState = "Done";
				}
				var item = '<tr class="warning">'
					+ '<td>' + quiz.quizCreateDate + '</td>'
					+ '<td>' + quiz.quizDeadline + '</td>'
					+ '<td>' + quiz.quizName+ '</td>'
					+ '<td>' + quiz.teaName + '</td>'
					+ '<td style="color:red">' + quizState + '</td>'+ '</tr>';
				$('#todoList').append(item);
			});
		}
		
	});
	
	$.getJSON(findHomework, function(data) {
		if (data.result && data.result.length > 0){
			var hwhead = '<tr><td><h6>Homeworks</h6></td></tr>' + '<tr>'
                			+ '<th scope="col">&nbsp;BeginTime</th>'
                			+ '<th scope="col">&nbsp;EndTime</th>'
                			+ '<th scope="col">&nbsp;Content</th>'
                			+ '<th scope="col">&nbsp;Publisher</th>'
                			+ '<th scope="col">&nbsp;State</th>' +'</tr>';
			$('#todoList').append(hwhead);
			$.each(data.result, function(index, homework) {
				var item = '<tr ';
				if (homework.state == 0) {
					item += 'class="warning">';
					homeworkState = "Todo";
				} else if (homework.state == 1) {
					item += 'class="success">';
					homeworkState = "Done";
				}
				item = item + '<td>' + homework.hwCreateDate + '</td>'
				+ '<td>' + homework.hwDeadline + '</td>'
				+ '<td> ' + homework.hwName + '</td>'
				+ '<td>' + homework.teaName + '</td>'
				+ '<td ';
				if (homework.state == 0)
					item += 'style="color:red">' + homeworkState + '</td>'+ '</tr>';
				else if (homework.state == 1)
					item += 'style="color:green">' + homeworkState + '</td>'+ '</tr>';
				
				$('#todoList').append(item);
			});
		}
	});
}

//$("#userPwd").formValidator();